package com.midian.action.mc.organization;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.midian.action.mc.common.AjaxAction;
import com.midian.common.annotation.FileModelDetails;
import com.midian.common.annotation.FunctionAnnotationOverride;
import com.midian.common.exception.BizException;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.PicUtils;
import com.midian.model.common.FileModel;
import com.midian.model.organization.OrganizationJoin;
import com.midian.model.organization.OrganizationType;
import com.midian.model.user.User;
import com.midian.service.organization.IOrganizationJoinService;
import com.midian.service.organization.IOrganizationTypeService;
import com.midian.service.pic.IPicService;
import com.midian.service.user.IUserService;

/**
 * 机构加盟Action
 * 
 * @author Mengfh
 *
 */
@Scope("prototype")
@Controller("organizationJoinAction")
@FunctionAnnotationOverride(isAction = true, auth = "机构加盟管理")
public class OrganizationJoinAction extends AjaxAction<OrganizationJoin> {
	/** 机构加盟实体 */
	private OrganizationJoin entity;
	/** 机构加盟实体列表 */
	private List<OrganizationJoin> rows;
	/** 机构加盟业务 */
	private IOrganizationJoinService organizationJoinService;
	/**机构logo*/
	private FileModel logo;
	/**机构营业执照*/
	private FileModel businessLicense;
	/**图片处理业务*/
	private IPicService picService;

	private Integer inspectStatus;
	
	public Integer getInspectStatus() {
		return inspectStatus;
	}

	public void setInspectStatus(Integer inspectStatus) {
		this.inspectStatus = inspectStatus;
	}
	
	
	
	@Resource(name="picService")
	public void setPicService(IPicService picService) {
		this.picService = picService;
	}
	
	@FileModelDetails(fieldName = "logo.id",isDelOldFile = false)
	public FileModel getLogo() {
		return logo;
	}

	public void setLogo(FileModel logo) {
		this.logo = logo;
	}
	
	@FileModelDetails(fieldName = "businessLicense.id",isDelOldFile = false)
	public FileModel getBusinessLicense() {
		return businessLicense;
	}

	public void setBusinessLicense(FileModel businessLicense) {
		this.businessLicense = businessLicense;
	}

	public OrganizationJoin getEntity() {
		return entity;
	}

	public void setEntity(OrganizationJoin entity) {
		this.entity = entity;
	}
	
	

	@Resource(name = "organizationJoinService")
	public void setOrganizationJoinService(IOrganizationJoinService organizationJoinService) {
		this.service = organizationJoinService;
		this.organizationJoinService = organizationJoinService;
	}

	@Override
	protected OrganizationJoin dytEntity() {
		if (this.entity == null) {
			this.entity = new OrganizationJoin();
		}
		return getEntity();
	}

	@Override
	protected void dyEntity(OrganizationJoin t) {
		setEntity(t);
	}

	@Override
	public void setRows(List<OrganizationJoin> rows) {
		this.rows = rows;
		
	}

	@Override
	public List<OrganizationJoin> getRows() {
		return rows;
	}

	/**
	 * 对加盟机构拒绝,修改delflag为2
	 * @return
	 */
	@Override
	public String disableEntity() {
		try {
			for (String id : ids) {
				OrganizationJoin organizationJoin = this.organizationJoinService.get(id);
				if (organizationJoin.getDelFlag()==IOrganizationJoinService.INSPECT_WAIT) {
					this.organizationJoinService.dispassInspectOrganizationJoin(ids, IOrganizationJoinService.INSPECT_FAIL);
					return SUCCESS;
				}
				
			}
		} catch (BizException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 对加盟机构审核通过，修改delflag为1
	 * @return
	 */
	@Override
	public String ableEntity() {
		try {
			for (String id : ids) {
				OrganizationJoin organizationJoin = this.organizationJoinService.get(id);
				if (organizationJoin.getDelFlag()==IOrganizationJoinService.INSPECT_WAIT) {
					this.organizationJoinService.dispassInspectOrganizationJoin(ids, IOrganizationJoinService.INSPECT_SUCCESS);
					return SUCCESS;
				}
				
			}
		} catch (BizException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**显示所有delFlag值的数据*/
	@Override
	public String showList() {
		if (inspectStatus==null) {
			if (this.entity == null) {
				this.entity = new OrganizationJoin();
			}
			this.entity.setDelFlag(null);
		}else {
			if (this.entity == null) {
				this.entity = new OrganizationJoin();
			}
			this.entity.setDelFlag(inspectStatus);
		}
		try {
			this.rows = this.organizationJoinService.showList(this.entity, this.pager, super.getOrder());
		} catch (BizException e) {
			getError(e);
		}
		return SUCCESS;
	}
	
}
